<template>
	<view class="Recharge_content Transfer_content">
		<view class="kuang">
			<view class="title">
				<view class="inline-block left">
					<image src="../../static/image/left.png" mode="" @tap="back"></image>
				</view>
			</view>
			<view class="vote_title">
				<image src="../../static/image/assets_icon.png"></image>
				<text>{{i18n.votetitle}}</text>
			</view>
			<view class="choice" style="margin-top: 72upx;" :style="i18n.choicestyle">
				<text>{{i18n.choice1}}</text>
				<input type="text" :style="i18n.inputwidth" v-model="assetName" :placeholder="i18n.choice1placeholder1" class="vote_num" v-bind:placeholder-style="i18n.issueinputstyle"  />
			</view>
			<text class="actual_asset_text">{{i18n.sjzcm}}：{{accountName}}:{{assetName}}</text>
			
			<view class="choice" :style="i18n.choicestyle">
				<text>{{i18n.choice22}}</text>
				<input type="text" :style="i18n.inputwidth" v-model="assetSymbol"  :placeholder="i18n.choice2placeholder" class="vote_num" v-bind:placeholder-style="i18n.issueinputstyle" />
			</view>
			<view class="choice" :style="i18n.choicestyle">
				<text>{{i18n.choice33}}</text>
				<input type="text" :style="i18n.inputwidth" v-model="initSupply" :placeholder="i18n.choice3placeholder"  class="vote_num" v-bind:placeholder-style="i18n.issueinputstyle" />
			</view>
			<view class="choice" :style="i18n.choicestyle">
				<text>{{i18n.choice4}}</text>
				<input type="text" :style="i18n.inputwidth" v-model="decimals" placeholder="" class="vote_num" :placeholder-style="i18n.issueinputstyle" />
			</view>
			<view class="choice" :style="i18n.choicestyle">
				<text>{{i18n.choice5}}</text>
				<input type="text" :style="i18n.inputwidth" v-model="limitation" :placeholder="i18n.choice5placeholder" class="vote_num" v-bind:placeholder-style="i18n.issueinputstyle" />
			</view>
			
			<text class="vote_encourage" style="margin-top: 15upx;" @tap="find_assets">{{i18n.voteencourage}}</text>
			
			<view class="bottombutton" @tap="issueAsset">
				{{i18n.bottombutton}}
			</view>
		</view>
		<password v-if="show" :gasPrice="100" :gasLimit="12000000" :yzpassword="i18n.yzpassword" :btnl="i18n.btn1" :btnr="i18n.btn2" :pwdtitle="i18n.pwdtitle" :passprice="i18n.passprice" :cap="i18n.cap" :placeholder="i18n.placeholder" @close="close" @callback="sendIssueAssetTx"></password>

	</view>
</template>

<script>
	import { ethers } from 'ethers';
	import * as oexWeb3 from 'oex-web3';
	import BigNumber from 'bignumber.js';
	import password from '../../components/password/password.vue';
	
	export default {
		computed: {
			i18n() {
				return this.$t('user')
			}
		},
		data() {
			return {
				accountName: "",
				assetName: "",
				assetSymbol: "",
				initSupply: "",
				decimals: "",
				limitation: "",
				show: false,
			}
		},
		components:{password},
		onLoad() {
			const accountInfo = uni.getStorageSync("account_info");
			this.accountName = accountInfo.accountName;
		},
		methods: {
			back(){
				this.$CommonJS.navigateBack()
			},
			find_assets(){
				this.$CommonJS.navigateTo('./assets_issue')
			},
			close(){
				//关闭支付框
				this.show = false
			},
			sendIssueAssetTx(privateKey, gasPrice, gasLimit) {
				const actionInfo = {
					accountName: this.accountName
				};
				const gasInfo = {gasPrice:"0x" + new BigNumber(gasPrice).toString(16), gasLimit: "0x" + new BigNumber(gasLimit).toString(16)};
				const payloadInfo = {assetName: this.accountName + ":" + this.assetName,
									 symbol: this.assetSymbol,
									 amount: new BigNumber(this.initSupply),
									 decimals: parseInt(this.decimals),
									 founder: this.accountName,
									 owner: this.accountName,
									 upperLimit: new BigNumber(this.limitation),
									 contractAccountName: null,
									 desc: "issued by offical wallet"}
				this.$Web3Wrapper.issueAsset(actionInfo, gasInfo, payloadInfo, privateKey, () => { this.show = false; }, (err) => {
					const _this=this
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '发生错误:':'An error occurred:' + err);
				});			
			},
			issueAsset() {
				const _this=this
				if(!this.assetName){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入资产名称':'Please enter asset name');
					return;
				}
				if(!this.assetSymbol){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入资产符号':'Please enter asset symbol');
					return;
				}
				if(!this.initSupply){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入初始发行的资产数量':'Please enter the number of assets initially issued');
					return;
				}
				if(!this.decimals){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入精度':'Please input the precision');
					return;
				}
				if(!this.limitation){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入资产发行上限':'Please enter the asset issuance limit');
					return;
				}
				var assetRegex = new RegExp('[a-z][a-z0-9]{1,15}');
				if(!assetRegex.test(this.assetName)){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入符合规范的资产名称':'Please enter a qualified asset name');
					return;
				}
				if(!assetRegex.test(this.assetSymbol)){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入符合规范的资产符号':'Please enter the asset symbol that meets the specification');
					return;
				}
				this.show = true;
			}
		}
	}
</script>

<style lang="stylus">
	@import "../../static/stylus/assets/assets.stylus"
	
</style>
